﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.采购委外订单;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.PurchaseCommand;
using SmartProduction.Read.API.Dto;

namespace SmartProduction.Read.API.Application.CommandHandlers.PurchaseCommandHandler
{
    public class PurchasedetailsQueryCommandHandler : IRequestHandler<PurchasedetailsQueryCommand, APIPaing<List<PurchasedetailsQueryDto>>>
    {
        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 采购
        /// </summary>
        private readonly IBaseRepository<Purchase> purchaseRepository;
        /// <summary>
        /// 详情
        /// </summary>
        private readonly IBaseRepository<Purchasedetails> purchaseDetailsRepository;

        public PurchasedetailsQueryCommandHandler(IMapper mapper, IBaseRepository<Purchase> purchaseRepository, IBaseRepository<Purchasedetails> purchaseDetailsRepository)
        {
            this.mapper = mapper;
            this.purchaseRepository = purchaseRepository;
            this.purchaseDetailsRepository = purchaseDetailsRepository;
        }

        public async Task<APIPaing<List<PurchasedetailsQueryDto>>> Handle(PurchasedetailsQueryCommand request, CancellationToken cancellationToken)
        {
            APIPaing<List<PurchasedetailsQueryDto>> result = new APIPaing<List<PurchasedetailsQueryDto>>();
            var list = from a in purchaseRepository.GetAll().Result
                       join b in purchaseDetailsRepository.GetAll().Result on a.OrderCode equals b.POrderCode
                       where a.IsDelete == 0
                       select new PurchasedetailsQueryDto
                       {
                           Purchaseid = a.Purchaseid,
                           OrderCode = a.OrderCode,
                           OrderName = a.OrderName,
                           OrderType = a.OrderType,
                           Project = a.Project,
                           Supplier = a.Supplier,
                           SignDate = a.SignDate,
                           Maker = a.Maker,
                           MakerDept = a.MakerDept,
                           Status = a.Status,
                           ApprovalStatus = a.ApprovalStatus,
                           AgreementCode = a.AgreementCode,
                           AgreementName = a.AgreementName,
                           OrderRemark = a.OrderRemark,
                           OrderAmount = a.OrderAmount,
                           Currency = a.Currency,
                           AdvancePayment = a.AdvancePayment,
                           OrderStartDate = a.OrderStartDate,
                           OrderEndDate = a.OrderEndDate,
                           SignTime = a.SignTime,
                           TaxType = a.TaxType,
                           TaxRate = a.TaxRate,
                           InvoiceAmount = a.InvoiceAmount,
                           PaidAmount = a.PaidAmount,
                           MaterialCode = b.MaterialCode,
                           MaterialName = b.MaterialName,
                           UnitPrice = b.UnitPrice,
                           Quantity = b.Quantity,
                           Unit = b.Unit,
                           Amount = b.Amount,
                           Abstract = b.Abstract,
                           MaterialId = b.MaterialId,
                           POrderCode = b.POrderCode,
                           CreatedBy = b.CreatedBy,
                           CreatedTime = b.CreatedTime,
                       };
            if (request.Purchaseid != null)
            {

                list = list.Where(o => o.Purchaseid == request.Purchaseid);
            }
            if (!string.IsNullOrEmpty(request.OrderCode))
            {

                list = list.Where(o => o.OrderCode == request.OrderCode);
            }
            if (!string.IsNullOrEmpty(request.OrderName))
            {
                list = list.Where(o => o.OrderName.Contains(request.OrderName));
            }
            if (!string.IsNullOrEmpty(request.Supplier))
            {

                list = list.Where(o => o.Supplier == request.Supplier);
            }
            if (request.OrderType != null)
            {

                list = list.Where(o => o.OrderType == request.OrderType);
            }
            if (request.Project != null)
            {

                list = list.Where(o => o.Project == request.Project);
            }
            if (request.ApprovalStatus!=null)
            {

                list = list.Where(o => o.ApprovalStatus == request.ApprovalStatus);
            }
            result.TotalCount = list.Count();
            result.PageCount = (int)Math.Ceiling((double)result.TotalCount / request.PageSize);
            list = list.OrderByDescending(o => o.Purchaseid).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            var data = mapper.Map<List<PurchasedetailsQueryDto>>(list);
            result.Code = APIEnums.查询成功;
            result.Msg = APIEnums.查询成功.ToString();
            result.data = data;
            return await Task.FromResult(result);
        }
    }
}
